package jpf.nextsearch.scanner;

import jpf.nextsearch.base.GenericObject;
import jpf.nextsearch.ds.model.HostVO;
import jpf.nextsearch.processing.processor.Processor;
import jpf.nextsearch.resourceprovider.handler.ResourceHandler;
import jpf.nextsearch.resourceprovider.impl.FtpResourceProvider;
import jpf.nextsearch.resourceprovider.impl.SmbResourceProvider;

/**
 * @author <a href="mailto:jacek.pruszkowski@gmail.com">Jacek Pruszkowski</a>
 */
public class HostScanProcessor extends GenericObject implements Processor<HostVO> {

	public static final int DEFAULT_FTP_PORT = 21;
	public static final int DEFAULT_FTP_CONNECTION_TIMEOUT = 5000; // 5 sec.
	public static final int DEFAULT_RESOURCES_PROCESSING_BATCH_SIZE = 1000;
	public static final String DEFAULT_FTP_USER = "anonymous";
	public static final String DEFAULT_FTP_PASSWORD = "anonymous";

	private int ftpPort = DEFAULT_FTP_PORT;
	private String ftpUser = DEFAULT_FTP_USER;
	private String ftpPassword = DEFAULT_FTP_PASSWORD;
	private int ftpConnectionTimeout = DEFAULT_FTP_CONNECTION_TIMEOUT;
	private boolean passiveMode = true;
	private boolean binaryMode = true;
	private int resourcesProcessingBatchSize = DEFAULT_RESOURCES_PROCESSING_BATCH_SIZE;

	private ResourceHandler resourceHandler;

	@Override
	public void process(HostVO data) throws Exception {
		if (isLoggingInfo()) {
			getLogger().info("Processing host : " + data.getIp());
		}

		scanFtp(data);
		scanSmb(data);

		if (isLoggingInfo()) {
			getLogger().info("Finished processing host : " + data.getIp());
		}

	}

	private void scanFtp(HostVO host) throws Exception {

		FtpResourceProvider resourceProvider = new FtpResourceProvider(host.getIp(), getFtpPort(), getFtpUser(), getFtpPassword(),
				isBinaryMode(), isPassiveMode(), getFtpConnectionTimeout());

		resourceProvider.connect();

		ScanResourceHandlerParams scanResourceHandlerParams = new ScanResourceHandlerParams();
		scanResourceHandlerParams.setHostId(host.getId());
		resourceProvider.passResourcesToHandler(getResourcesProcessingBatchSize(), getResourceHandler(), scanResourceHandlerParams);
		resourceProvider.close();
	}

	private void scanSmb(HostVO host) throws Exception {
		SmbResourceProvider resourceProvider = new SmbResourceProvider(host.getIp());

		resourceProvider.connect();

		ScanResourceHandlerParams scanResourceHandlerParams = new ScanResourceHandlerParams();
		scanResourceHandlerParams.setHostId(host.getId());
		resourceProvider.passResourcesToHandler(getResourcesProcessingBatchSize(), getResourceHandler(), scanResourceHandlerParams);
	}

	public void setFtpPort(int ftpPort) {
		this.ftpPort = ftpPort;
	}

	public int getFtpPort() {
		return ftpPort;
	}

	public void setFtpUser(String ftpUser) {
		this.ftpUser = ftpUser;
	}

	public String getFtpUser() {
		return ftpUser;
	}

	public void setFtpPassword(String ftpPassword) {
		this.ftpPassword = ftpPassword;
	}

	public String getFtpPassword() {
		return ftpPassword;
	}

	public void setPassiveMode(boolean passiveMode) {
		this.passiveMode = passiveMode;
	}

	public boolean isPassiveMode() {
		return passiveMode;
	}

	public void setBinaryMode(boolean binaryMode) {
		this.binaryMode = binaryMode;
	}

	public boolean isBinaryMode() {
		return binaryMode;
	}

	public void setFtpConnectionTimeout(int ftpConnectionTimeout) {
		this.ftpConnectionTimeout = ftpConnectionTimeout;
	}

	public int getFtpConnectionTimeout() {
		return ftpConnectionTimeout;
	}

	public void setResourcesProcessingBatchSize(int resourcesProcessingBatchSize) {
		this.resourcesProcessingBatchSize = resourcesProcessingBatchSize;
	}

	public int getResourcesProcessingBatchSize() {
		return resourcesProcessingBatchSize;
	}

	public void setResourceHandler(ResourceHandler resourceHandler) {
		this.resourceHandler = resourceHandler;
	}

	public ResourceHandler getResourceHandler() {
		return resourceHandler;
	}

}
